package la.bbs558.controller.user;

import cn.dev33.satoken.annotation.SaIgnore;
import la.bbs558.entity.AppDownload;
import la.bbs558.entity.params.ExclusiveVisitRecordParams;
import la.bbs558.service.AppDownloadService;
import la.bbs558.service.ExclusiveVisitRecordService;
import la.bbs558.util.IpUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: Shanks
 * @date: 2024/1/19 19:14
 * @desc:
 */
@Controller
@RequestMapping("")
public class ThymeleafController {

    @Resource
    private ExclusiveVisitRecordService exclusiveVisitRecordService;

    @Resource
    private AppDownloadService appDownloadService;

    @GetMapping("/exclusive/{code}")
    @SaIgnore
    public String exclusiveLink(@PathVariable("code") String code, Map<String, Object> map) {
        map.put("code", code);
        return "device-info";
    }

    @GetMapping("/exclusive/device-info/get")
    @SaIgnore
    public String getDeviceInfo(ExclusiveVisitRecordParams recordParams, HttpServletRequest request) {
        // 获取用户的真实IP
        recordParams.setIp(IpUtil.getIp(request));
        exclusiveVisitRecordService.saveRecord(recordParams);
        if ("移动端".equals(recordParams.getSource())) {
            return "redirect:https://558.la/download-app";
        }
        return "redirect:https://558.la";
    }

    @GetMapping("/download")
    @SaIgnore
    public String download(Map<String, Object> map) {
        AppDownload appDownload = appDownloadService.lambdaQuery()
                .eq(AppDownload::getStatus, 1)
                .last("limit 1")
                .oneOpt()
                .orElse(new AppDownload().setUrl(""));
        map.put("downloadUrlAndroid", appDownload.getUrl());
        return "download";
    }

}
